﻿/*
    global vara.
 */

var divEcolor = "#037";
var globals;
var mouseover;
var clrcheck = true;
var isGo = false;
var goRed = "", goBlack = "";
var goCurChess = null,
    goRedChess, goBlackChess;
var gIsRectBoard = false;
var gHasSize = false;
var gHasPC = false;

mouseoverrectinit( boardrects );
function mouseoverrectinit( rc )
{
    mouseover = rc; // fcscript_init(rc);
}

function mouseovereventscriput( x, y )
{
    /*
    for ( var i=0; i<mouseover.length; i++ ) {

        if (mouseover[i].length == 5 &&
            mouseover[i][0] == '-') {
            if (x >= 30 + 60 * mouseover[i][1] && x <= 30 + 60 * mouseover[i][2] &&
                y >= 30 + 60 * mouseover[i][3] && y <= 30 + 60 * mouseover[i][4]) {
                return; // not show cursor.
            }
        }
    }

    for ( var i=0; i<mouseover.length; i++ ) {

        if (mouseover[i].length == 5 &&
            mouseover[i][0] == '+') {
            if (x >= 30 + 60 * mouseover[i][1] && x <= 30 + 60 * mouseover[i][2] &&
                y >= 30 + 60 * mouseover[i][3] && y <= 30 + 60 * mouseover[i][4])
            {
    */          var pos = getChessPos(x, y)
    if ( mouseover.charAt( pos.r * g_col + pos.c ) != '0' )
        return;
                if (clicknum % 2 == 0 && !isGo) {
                    moveBox(box2, pos)
                }
                else {
                    moveBox(box, pos)
                }
    /*        return; // show it.
            }
        }
    }*/
}

var paperslst = null;
var curpaperidx = -1;
var formshowtimes = -1;
var formclick1sttime = -1;

function RegCodeRWcmd( cmd )
{
    var xmlhttp;
    if (window.XMLHttpRequest)
        xmlhttp=new XMLHttpRequest();
    else
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            var resp = xmlhttp.responseText;
            if ( resp.substr( resp.length-4, 4 ) == '<br>' )
                resp = resp.substr( 1, resp.length - 5 );
            var regcode = getQueryString( "code" );
            if ( resp.length >= 10 ) {
                // set the readonly.
                if ( regcode == resp ) {
                    var id = getQueryString( "id" );
                    RegCodeRWcmd( "?id=" + id );
                }
            } else
            if ( resp.length == 6 ) {
                // set the readonly.
                if ( document.getElementById( "kw" ).value == resp )
                {
                    var id = getQueryString( "id" );
                    var cmd = "?id=" + id + "&code=" + resp;
                    var pn = getQueryString( "p" );
                    if ( pn != null && pn != "" )
                        cmd += "&p=" + pn;
                    window.location.href = cmd;
                }
            }
        }
    }

    xmlhttp.open("GET","fcregrw.php" + cmd,true);
    xmlhttp.send();
}

var besetreadonly = false;

function RegCodeRW( code ) // if regstr in url and code = 6 is write, else is compare.
{
    var xmlhttp;
    if (window.XMLHttpRequest)
        xmlhttp=new XMLHttpRequest();
    else
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            var resp = xmlhttp.responseText;
            if ( resp.substr( resp.length-4, 4 ) == '<br>' )
                resp = resp.substr( 1, resp.length - 5 );
            var regcode = getQueryString( "code" );
            if ( resp.length >= 10 ) {
                // set the readonly.
                besetreadonly = true;
                if ( regcode == resp ) {
                    hiddenyuandshowform();
                }
            } else
            if ( resp.length == 6 ) {
                // set the readonly.
                if ( resp == regcode )
                {   // set writeable.
                    besetreadonly = false;
                } else
                // set the readonly.
                    besetreadonly = true;
            }
        }
    }


    var idstr = getQueryString( "id" );

    if ( idstr != null && idstr != "" )
    {

        var cmdstr = "";
        if ( code.length == 6 )
        {
            cmdstr = "fcregrw.php?id=" + idstr;
        } else
        if ( code.length == 0 ) {
            cmdstr = "fcregrw.php?id=" + idstr;
        } else
        if ( code.length == 10 || code.length == 11 )
        {
            cmdstr = "fcregrw.php?id=" + idstr + "&reg=" + code;
        } else
            return;

        xmlhttp.open("GET",cmdstr,true);
        xmlhttp.send();
    }
}

function HasRegCodeModify()
{
    var id = getQueryString( "id" );
    if ( id == null || id == "" ) return false;
    var regstr = getQueryString( "code" );
    if ( regstr == null || regstr == "" )
        regstr = "";
    else
    if ( regstr.length != 10 && regstr.length != 11 )
    {
        var code = getQueryString( "reg" );
        if ( code != null && code.length != 10 && code.length != 11 )
            return false;
    }

    RegCodeRW( regstr );
    return true;
}

function returnatform( formid, formidx, inputid )
// "search-form-new", 0, "kw"
{
    var code = document.getElementById( inputid ).value;
// another method of get input code.
//    var code3 = document.forms[ formidx ].code.value;
//    var code4 = document.forms[ formid ].code.value;
//

    var id = getQueryString( "id" );
    var cmd = "";
    if ( id != null && id != "" )
        cmd += "?id=" + id;
    var pn = getQueryString( "p" );
    if ( pn != null && pn != "" )
        cmd += "&p=" + pn;
    if ( code != null && code != "" )
    {
        var regstr = getQueryString( "code" );
        if ( regstr != null && (
            regstr.length == 10 ||
            regstr.length == 11 ) )
        {
            if ( cmd == "" )
                cmd += "?";
            else
                cmd += "&"
            cmd += "i=" + code + "&reg=" + regstr;
            RegCodeRWcmd( cmd );
            return;
        } else
        if ( code.length == 6 ||
            code.length == 10 ||
            code.length == 11 ) {
            if ( cmd == "" )
                cmd += "?";
            else
                cmd += "&"
            cmd += "code=" + code;
        } else
            return;
    }
    if ( cmd != null && cmd != "" )
        window.location.href = cmd;
}

function hiddenyuandshowform()
{
    var divs = document.getElementById("search-form-new");
    divs.style.display = "block";
    divs.focus();
//                        divs.innerText = '0';
    var divd = document.getElementById("divd");
    divd.style.display = "none";

    divs.onsubmit = function (e) {
        var e = e || window.event;
        if (e.preventDefault) e.preventDefault();
        else window.event.returnValue = false;
//        returnatform( "search-form-new", 0, "kw" );
    }
}

function showyuandhiddenform()
{
    var divs = document.getElementById("search-form-new");
    divs.style.display = "none";
    divs.focus();
//                        divs.innerText = '0';
    var divd = document.getElementById("divd");
    divd.style.display = "block";
}

initglobal();
function initglobal() {

    globals = fcscript_init(globalscript);

    if (globals[0].length != 1)
        msgalert("'#037;' + // var divEcolor." + 'length != 1', -1);
    else {
        divEcolor = globals[0][0];
        alertclear();
    }

    if (globals[1].length != 1)
        msgalert("transcube.png, show length != 1", -1);
    else if (globals[1][0] == 'false') {
        // hidden.
        var div = document.getElementById("divx");
        div.style.display = 'none';
    } else if (globals[1][0] == 'true') {
        // show.
        var div = document.getElementById("divx");
        div.style.display = 'block';
    }

    if (globals[2].length != 2)
        msgalert("yu100.png, show length != 2", -1);
    else if (globals[2][0] == 'false') {
        // hidden.
        var div = document.getElementById("divd");
        div.style.display = 'none';
    } else if (globals[2][0] == 'true') {
        // show.
        var div = document.getElementById("divd");
        div.style.display = 'block';
        if ( globals[2][1] != "" ) {
            if ( globals[2][1].length > 1 )
                div.innerHTML = "<a href=" + '"' + globals[2][1] + '">' + "<img src=" + '"' + "./1_files/yu100.png" + '"' + "/></a>";
            else {
                showyuandhiddenform();
                HasRegCodeModify();

                formshowtimes = parseInt(globals[2][1], 10);
                div.innerHTML = "<img src=" + '"' + "./1_files/yu100.png" + '"' + "/>";

        if ( getQueryString("p") == "258" )
        {
                div.addEventListener( 'click', function () {
                    hd258();
                }, false)

                div.addEventListener( 'dblclick', function () {
                    hd258();
                }, false)
        } else 
        {

//                setInterval(function () {
//                    formclick1sttime = 0;
//                }, 2000);
                // adding event click to diveyu100.
                div.addEventListener( 'click', function () {

//                    if ( formclick1sttime >= 0 )
//                        formclick1sttime ++;

//                    if ( formclick1sttime >= formshowtimes )
//                    {
                    hiddenyuandshowform();
//                    }

                }, false)

                div.addEventListener( 'dblclick', function () {

//                    if ( formclick1sttime >= 0 )
//                        formclick1sttime += 2;

//                    if ( formclick1sttime >= formshowtimes )
//                    {
                    hiddenyuandshowform( );
//                    }

                }, false)
          }

            }
        } else
            div.innerHTML = "<img src=" + '"' + "./1_files/yu100.png" + '"' + "/>";
    }

    if (globals[3].length != 2)
        msgalert("<title>火焰棋#40.谁先走出广场2vs2</title>, show length != 2", -1);
    else {
        document.title = "火焰棋#" + globals[3][0] + "." + globals[3][1];
    }

    if (globals[4].length != 2)
        msgalert("小白③号, show length != 2", -1);
    else {
        var div = document.getElementById("divc");
        div.innerText = globals[4][0]; // "小白③号"

        paperslst = fcscript_init(fcscriptpapers);
        curpaperidx = 0;

        var pi = getQueryString("p");
        if ( pi == "258" ) { pi="1"; globals[3][0] = "258"; }
        if (pi != null && pi >= 0 && 
            paperslst != null && pi < paperslst.length ) {
            curpaperidx = pi;
            div.innerText = paperslst[curpaperidx][1];

            var id = getQueryString("id");
            if ( id != null ) {
                document.title = "火焰棋#" + globals[3][0] + ".#" + id + ".云棋盘3.0" + paperslst[curpaperidx][1];
            }

        }

        // adding event click to divec.
        div.addEventListener('click', function () {
            alertclear();
            msgalert( globals[4][1], -1 );

            if ( paperslst != null )
            {
//        if (idx == 3) {
//    msgalert( "logo#" + idx.toString() + "被按下！");
                if (curpaperidx == -1)
                    curpaperidx = 0;
                else if (curpaperidx >= paperslst.length - 1 )
                    curpaperidx = 0;
                else
                    curpaperidx++;

                var msg = paperslst[curpaperidx][1];

                // hidden the hexpaper.
                var p = document.getElementById("hexpaper");
                p.src = "1_files/" + paperslst[curpaperidx][0];
//            msgalert(/*"显示" +*/ msg + paperslst.length + p.src , 0);

                var div = document.getElementById("divc");
                div.innerText = paperslst[curpaperidx][1]; // "小白③号"

//        }
            }


        }, false)

    }

    if (globals[5].length > 0) {
        var div = document.getElementById("diva");
        for (var i = 0; i < globals[5].length; i++) {
            if ( globals[5][i].indexOf('.') == -1 )
            div.innerHTML += "<img src = " + '"' + "./1_files/" + globals[5][i] + '.png"'+ '/ ><br>'
            else
            div.innerHTML += "<img src = " + '"' + "./1_files/" + globals[5][i] + '"'+ ' height="90" width="90" / ><br>'
        }
        div.onclick = function(e) {
            var idx = Math.floor( e.y / 100 );
            if ( idx * 100 - 8 < e.y && e.y < (idx+1) * 100 - 8 &&
                 8 < e.x && e.x < 100-8 ) {
                // in item middle.
                fcscript_logo_onclick( idx );
            }
        };
    }

    if (globals[6].length != 1)
        msgalert("clrcheck, show length != 1", -1);
    else if (globals[6][0] == 'false')
        clrcheck = false;
    else if (globals[6][0] == 'true')
        clrcheck = true;

    if (globals[7].length != 3)
        msgalert("'go,z,Z;'   // no move, r=z, b=Z,", -1);
    else if (globals[7][0] == '')
        isGo = false;
    else if (globals[7][0] == 'go') {
        isGo = true;
        goRed = globals[7][1];
        goBlack = globals[7][2];
    }

    if (globals[8].length != 1)
        msgalert("'rect;'  // hex or rect", -1);
    else if (globals[8][0] == 'rect')
        gIsRectBoard = true;
    else if (globals[8][0] == 'hex') {
        gIsRectBoard = false;
    }

    if (globals[9].length != 1)
        msgalert("'size;'  // hex or rect", -1);
    else if (globals[9][0] == 'size')
        gHasSize = true;

    if (globals[10].length != 1)
        msgalert("'pc;'  // hex or rect", -1);
    else if (globals[10][0] == 'size')
        gHasPC = true;

    if (globals[11].length != 3)
        msgalert("'CCG,fc100;'// doc.pdf, wav.mp3, video.mp4", -1);
    else {
        var div = document.getElementById("divp");
        var pdf = getQueryString( "pdf");
        if ( pdf != null && pdf != "" ) {
            //if ( pdf.substr( pdf.length-4, 4 ) != ".pdf" )
            if ( pdf.indexOf( "." ) == -1 )
                div.innerHTML += "<a href=" + '"' + "./1_files/" + pdf + '.pdf"' + '" target="_blank">' + "<img src=" + '"' + "./1_files/pdf.png" + '"' + " height='60' width='40' /></a>";
            else
                div.innerHTML += "<a href=" + '"' + pdf + '" target="_blank">' + "<img src=" + '"' + "./1_files/pdf.png" + '"' + " height='60' width='40' /></a>";
        } else
        if ( globals[11][0].length > 1 ) {
            // if ( globals[11][0].substr( globals[11][0].length-4, 4 ) != ".pdf" )
            if ( globals[11][0].indexOf( "." ) == -1 )
                div.innerHTML += "<a href=" + '"' + "./1_files/" + globals[11][0] + '.pdf"' + '" target="_blank">' + "<img src=" + '"' + "./1_files/pdf.png" + '"' + " height='60' width='40' /></a>";
            else
                div.innerHTML += "<a href=" + '"' + globals[11][0] + '" target="_blank">' + "<img src=" + '"' + "./1_files/pdf.png" + '"' + " height='60' width='40' /></a>";
        }
        var mp3 = getQueryString( "mp3");
        if ( mp3 != null && mp3 != "" ) {
            //if ( mp3.substr( mp3.length-4, 4 ) != ".mp3" )
            if ( mp3.indexOf( "." ) == -1 )
                div.innerHTML += "<a href=" + '"' + "./1_files/" + mp3 + '.mp3"' + '" target="_blank">' + "<img src=" + '"' + "./1_files/mp3.png" + '"' + " height='60' width='40' /></a>";
            else
                div.innerHTML += "<a href=" + '"' + mp3 + '" target="_blank">' + "<img src=" + '"' + "./1_files/mp3.png" + '"' + " height='60' width='40' /></a>";
        } else
        if ( globals[11][1].length > 1 ) {
            //if ( globals[11][1].substr( globals[11][1].length-4, 4 ) != ".mp3" )
            if ( globals[11][1].indexOf( "." ) == -1 )
                div.innerHTML += "<a href=" + '"' + "./1_files/" + globals[11][1] + '.mp3"' + '" target="_blank">' + "<img src=" + '"' + "./1_files/mp3.png" + '"' + " height='60' width='40' /></a>";
            else
                div.innerHTML += "<a href=" + '"' + globals[11][1] + '" target="_blank">' + "<img src=" + '"' + "./1_files/mp3.png" + '"' + " height='60' width='40' /></a>";
        }
        var mp4 = getQueryString( "mp4");
        if ( mp4 != null && mp4 != "" ) {
            //if ( mp4.substr( mp4.length-4, 4 ) != ".mp4" )
            if ( mp4.indexOf( "." ) == -1 )
                div.innerHTML += "<a href=" + '"' + "./1_files/" + mp4 + '.mp4"' + '" target="_blank">' + "<img src=" + '"' + "./1_files/mp4.png" + '"' + " height='60' width='40' /></a>";
            else
                div.innerHTML += "<a href=" + '"' + mp4 + '" target="_blank">' + "<img src=" + '"' + "./1_files/mp4.png" + '"' + " height='60' width='40' /></a>";
        } else
        if ( globals[11][2].length > 1 ) {
            // if ( globals[11][2].substr( globals[11][2].length-4, 4 ) != ".mp4" )
            if ( globals[11][2].indexOf( "." ) == -1 )
                div.innerHTML += "<a href=" + '"' + "./1_files/" + globals[11][2] + '.mp4"' + '" target="_blank">' + "<img src=" + '"' + "./1_files/mp4.png" + '"' + " height='60' width='40' /></a>";
            else
                div.innerHTML += "<a href=" + '"' + globals[11][2] + '" target="_blank">' + "<img src=" + '"' + "./1_files/mp4.png" + '"' + " height='60' width='40' /></a>";
        }
        if ( globals[11][0].length > 1 || globals[11][1].length > 1 )
            div.innerHTML += '<br>'
    }
}

function getChessTypeScript(ch) {
//    for ( var i=0; i<fcs.length; i++ )
//        if ( ch == fcs[i].r || ch == fcs[i].b) return fcs[i].name;
    var fsc = fcscript_init( fcscript );
    i = 0;
    while ( i < fsc.length ) {
        if ( ch == fsc[i][0] || ch == fsc[i][1] ) return { n:fsc[i][2], c:fsc[i][4] };
        i+=parseInt( fsc[i][3] ) + 1;
    }
    return { n:null, c:null }
}

function fcscript_init( script )
{
    var last = 0;
    for ( var i=0; i<script.length; i++ )
        if ( script.charAt(i) == ';' ) last ++;
    var sarray = new Array(last);
    last = 0;
    var parray = 0;
    for ( var i=0; i<script.length; i++ )
        if ( script.charAt(i) == ';' ) {
            var cmds = script.substring( last, i )+',';
            var cmdlast = 0;
            for ( var j=0; j<cmds.length; j++ )
                if ( cmds.charAt(j) == ',' ) cmdlast ++;
            // create the ln array.
            sarray[parray] = new Array(cmdlast);
            cmdlast = 0;
            var pcmd = 0;
            for ( var j=0; j<cmds.length; j++ )
                if ( cmds.charAt(j) == ',' ) {
                    sarray[parray][pcmd] = cmds.substring( cmdlast, j );
                    cmdlast = j + 1;
                    pcmd ++;
                }
            last = i+1;
            parray ++;
        }
    return sarray;
}

function alertclear()
{
    var dive = document.getElementById("dive");
    dive.innerText = "";
    dive.style.color = divEcolor;
}

function getalert()
{
    var dive = document.getElementById("dive");
    var ret = dive.innerText;
    ret = ret.replace(/[\r\n]/g," ");
    return ret;
}

function msgalert( msg, ret )
{
    var dive = document.getElementById("dive");
    var oldmsg = dive.innerHTML;
    if ( oldmsg != "" ) oldmsg += "<br>";
    dive.innerHTML = oldmsg + msg;
//    alert( msg );
//    getalert();
    return ret;
}

function msgalertlines( msg, ln )
{
    return;
    var dive = document.getElementById("divf");
    var oldmsg = dive.innerHTML;
    var i = 0, j, count = 0;
    while ( (j = oldmsg.indexOf('<br>',i)) != -1 )
    {
        i = j+1;
        count ++;
    }
    if ( count < ln ){
        //    msgalert( msg, true );
    } else {
        j = oldmsg.lastIndexOf("<br>");
        oldmsg = oldmsg.substring( 0, j ); //, oldmsg.length );
        //    msgalert( msg, true );
        j = oldmsg.lastIndexOf("<br>");
        oldmsg = oldmsg.substring( 0, j ); //, oldmsg.length );
    }

    if ( oldmsg != "" ) oldmsg = "<br>" + oldmsg;
    dive.innerHTML = msg + oldmsg;
}

function condition3_getvar( local, v )
{
    switch (v) {
        case 'ar': return local.ar;
        case 'ac': return local.ac;
        case 'dc': return local.dc;
        case 'dr': return local.dr;
        case 's.color': return local.s.color;
        case 't.color': return local.t.color;
        case 't': return local.t;
        case 't.type': return local.t.type;
        case 'pos.r': return local.pos.r;
        case 'pos.c': return local.pos.c;
        case 'jump' : return jumpcheck( local.prepos, local.pos );
        case 'jumpX' : return jumpXcheck( local.prepos, local.pos );
    }
    if ( condition3_isnumber( v ) ) return parseInt( v );
    if ( v == "undefined") return undefined;
    return v; // msgalert( "invalid varable "+v, -1 );
           // return str directly.
}

function condition3_isnumber( v )
{
    var p = 0;
    if (v.charAt(0) == '-' ) p++;
    while ( p < v.length ) {
        if (v.charAt(p) < '0' || v.charAt(p) > '9' ) return false;
        p++;
    }
    return true;
}

function condition3_fcs( local, sarray3 )
{
    if ( sarray3.length != 3 ) return false;
    var A = condition3_getvar( local, sarray3[1] );
    var B = condition3_getvar( local, sarray3[2] );
    switch ( sarray3[0] ) {
        case '>':
            return A > B;
            break;
        case '>=':
            return A >= B;
            break;
        case '==':
            return A == B;
            break;
        case '!=':
            return A != B;
            break;
        case '<=':
            return A <= B;
            break;
        case '<':
            return A < B;
            break;
        default:
            return msgalert( "非正常的表达式" + sarray3[0] + "," + sarray3[1] + "," + sarray3[2], false );
    }
}

function fcscript_trans( prepos, pos )
{
    var s = ( isGo && prepos == null )
        ? createGoS( ( clicknum%2 == 0 ) ? goRed : goBlack )
        : matrix[prepos.r][prepos.c]
    var t  = matrix[pos.r][pos.c]
    var dr = pos.r - s.r;   // prepos.r
    var dc = pos.c - s.c;   // prepos.c
    var ar = (dr > 0) ? dr : -dr
    var ac = (dc > 0) ? dc : -dc

    var local = {
        prepos:prepos,
        pos:pos,
        s:s,
        t:t,
        dr:dr,
        dc:dc,
        ar:ar,
        ac:ac
    };

    // todo scrtpt.
    var sarray = fcscript_init( fcscript );
    for ( var j=0; j<sarray.length; j++ )
    {
        if ( sarray[j][2] != s.type )
        {
            j += parseInt( sarray[j][3] );

        } else {
     //       j ++;

    for ( var i=j+1; i<parseInt( sarray[j][3] )+j+1;i++ ) {
        // explain..each condition.
        if ( sarray[i].length != 4 )
        {
            // alert the script error, to break.
            var msg = "";
            for ( var j=0; j<sarray[i].length; j++ )
                msg += sarray[i][j] + ',';
            return msgalert( "脚本格式不对，请查看" + msg, -10 );
        } else {
            if ( sarray[i][0] == "||" ) {
                var b = false;
                for ( var k=0; k<parseInt(sarray[i][1]); k++ )
                    if ( condition3_fcs( local, sarray[i+k+1] ) ) b = true;
                if ( b )
                    return msgalert( sarray[i][3], sarray[i][2] );
            } else
            if ( sarray[i][0] == '&&' ) {
                var b = true;
                for ( var k=0; k<parseInt(sarray[i][1]); k++ )
                    if ( !condition3_fcs( local, sarray[i+k+1] ) ) { b = false; break; }
                if ( b )
                    return msgalert( sarray[i][3], sarray[i][2] );
            } else
                return msgalert( "非正常脚本命令" + sarray[i][0], -10 );
        }
        i += parseInt(sarray[i][1]);

    }
            j += parseInt( sarray[j][3] );
        }

    }

    // return {r:r, c:c}
    // pos.r|c == chess.r|c.
    //----------------------------------------------
    //一次只能一步，不能后退
//    if (ar > 1 || ac > 1) return -5
//    if (s.color == 'b' && dr == -1) return -5;  // 黑棋不能向上移动。
//    if (s.color == 'r' && dc == -1) return -5;  // 红棋不能向左移动。
    // 不能斜45度移动。
//    if ( ar == 1 && ac == 1 ) return -5;
    // 不能移动到对方棋子，不能吃子。
//    if ( t != undefined && t.type == "zu") return -3;
    // 不能移动到对方的目的地。
//    if (s.color == 'b' && pos.c == 6 ) return -5;
//    if (s.color == 'r' && pos.r == 3 ) return -5;
    return 0
}

/*
function fcsCheckMove(prepos,pos)
{

//    for ( var i=0; i<fcs.length; i++ )
//    if (type == fcs[i].name ) {   // "zu"
        return fcscript_trans( prepos, pos );
        // return zumove( prepos, pos );
//    }
}
*/

function jumpcheck(prepos, pos){
    return countLineChess( prepos, pos );
}

function jumpXcheck(prepos, pos){
    return countXLineChess( prepos, pos );
}

function checkFinish( clr ) {
    jcheckFinish( clr );
/*
    var count = 0;
    var msg = "";
    if ( clr == 'b' ) {
        var b1 = matrix[4][0];
        var b2 = matrix[4][1];
        var b3 = matrix[4][2];
        var b4 = matrix[4][3];
        msg = "黑";
    } else
    if ( clr == 'r') {
        var b1 = matrix[0][4];
        var b2 = matrix[1][4];
        var b3 = matrix[2][4];
        var b4 = matrix[2][4];
        msg = "红";
    }
    if ( b1 != null ) count ++;
    if ( b2 != null ) count ++;
    if ( b3 != null ) count ++;
    if ( b4 != null ) count ++;
    if ( count == 3 ) {
        msg += "方胜利，3枚棋子走出光场。"
        msgalert( msg, 0 );
    }
 */
}

function getQueryString(name) {
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return decodeURI(r[2]); return null;
}


var replaylst = null;
var replaylns = 0;
var replaylnbuffer = null;

function initReplayLst( param ) {
    replaylst = fcscript_init( param );
}

var replaypos = -1;

function ReplayChessMove( pos )
{
    if ( pos.c < 7 ) {
        // left;
        replaypos --;
    } else
    {
        // right;
        replaypos ++;
    }

    if ( replaypos < 0 )
        replaypos = 0;

    if ( replaypos > replaylst.length )
        replaypos = replaylst.length;

    // replay.
    replaylns = 0;
    do {
        ReplayChessMoveOld();
    } while ( replaylns < replaypos );

    alertclear();
    msgalert( replaypos + "/" + replaylst.length + ": " + replaylnbuffer.substr( 14, replaylnbuffer.length-14*2 ), 0 )

    RecordChessMove(replaylnbuffer );
}

function ReplayChessMoveOld( )
{
    if ( replaylns < 0 ) return;

    if ( replaylns == 0 ) {
        replaylnbuffer = "";
        var plst = 0 ;

        while ( plst < replaylst[0].length ) {

            for ( var i = 0; i<replaylst[0][plst]; i++ )
                replaylnbuffer += '0';

            plst ++;

            if ( plst < replaylst[0].length ) {
                if ( replaylst[0][plst][0] != '0' )
                {
                    replaylnbuffer += replaylst[0][plst];
                    plst ++;
                }
            }

        }
//        RecordChessMove(replaylnbuffer );
        replaylns ++;
    }
    else
    if ( replaylns > 0 && replaylns < replaylst.length )
    {  // do change.
        var plst = 0 ;
        var strstart = 0;
        var newbuf = "";

        while ( plst < replaylst[replaylns].length ) {
            newbuf += replaylnbuffer.substring( strstart, parseInt(replaylst[replaylns][plst],10)-1 );
            strstart = parseInt( replaylst[replaylns][plst], 10 );// + 1;
            plst ++;
            if ( plst < replaylst[replaylns].length )
            {   newbuf += replaylst[replaylns][plst]; plst ++; }
        }
        if ( strstart < replaylnbuffer.length )
            newbuf += replaylnbuffer.substring( strstart, replaylnbuffer.length );

        replaylnbuffer = newbuf;
//        RecordChessMove(replaylnbuffer );
        replaylns ++;
    }
}

var iCountInit4press = -1;
var keysCountInit4press = [
    (g_row-1)*g_col+0,
    (g_row-1)*g_col+1,
    (g_row-1)*g_col+g_col-2,
    (g_row-1)*g_col+g_col-1];

function CountInit4press( pos )
{
    var p = pos.r * g_col + pos.c;
    if ( iCountInit4press <= 0 )
        iCountInit4press = 0;
    if ( keysCountInit4press[iCountInit4press] == p )
    {
        iCountInit4press ++;
        msgalert( iCountInit4press + "/4", 1 );
    }
    if ( iCountInit4press == keysCountInit4press.length )
    {
        iCountInit4press = 0;
        RecordChessMove( chesspos );
        // location.reload();
        return true;
    } else
    if ( iCountInit4press > keysCountInit4press.length )
        iCountInit4press = 0;
    return false;
}